package com.javalong.mir.service;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.javalong.mir.dao.UserDao;
import com.javalong.mir.pojo.User;

@Service
public class UserService {

	@Autowired
	private UserDao userDao;
	
	public List<User> getList(int page,int pageSize){
		return userDao.findAll(page, pageSize);
	}
	
	public void save(User user){
		userDao.save(user);
	}
	
	public User findUserByOpenid(String openid){
		return userDao.findUserByOpenid(openid);
	}
	
	public int getCount(){
		return userDao.getCount();
	}
	
	public User findUserById(int id){
		return userDao.findById(id);
	}
	
	public void delete(int id){
		userDao.delete(id);
	}
	
	public void updateUser(User user){
		user.setUpdateAt(new Date());
		userDao.update(user);
	}
	
	public User saveUserIfnotExists(User user){
		if(user!=null&&StringUtils.isEmpty(user.getOpenid())){
			User u = findUserByOpenid(user.getOpenid());
			if(u==null){
				save(user);
				return user;
			}else
				return u;
		}
		return null;
	}

	/**
	 * 获取爱心机构
	 * @param pageIndex
	 * @param pageSize
	 * @return
	 */
	public List<User> getMechinismList(int pageIndex, int pageSize) {
		return userDao.findMechinismList(pageIndex,pageSize);
	}
	
	/**
	 * 获取爱心人士
	 * @param pageIndex
	 * @param pageSize
	 * @return
	 */
	public List<User> getUserList(int pageIndex, int pageSize) {
		return userDao.findUserList(pageIndex,pageSize);
	}

	/**
	 * 通过openid获取到用户的信息
	 * @param openid
	 * @return
	 */
	public User getUserByOpenid(String openid) {
		return userDao.findUserByOpenid(openid);
	}
}
